
#include <iostream>
#include <stdio.h>
using namespace std;
typedef struct PolyNode
{
    int exp;
    int ceof;
    PolyNode *next;
    /* data */
} PolyNode;

class PolyList
{
private:
    PolyNode *head;
    /* data */
public:
    void addPolyNode(PolyNode *newPolyNode);
    PolyList(PolyNode *p);
    void printPolyList();
    ~PolyList();
};
//初始化
PolyList::PolyList(PolyNode *p)
{
    //p为头部节点
    *head = *p;
}
void PolyList::addPolyNode(PolyNode *p)
{
        //当前节点为头节点
    PolyNode *currentNode = head;
    //1 3 5 7 9 11 
    // 6
    //    |
    while (currentNode->next != NULL && currentNode->next->ceof<=p->ceof )
    {
        currentNode = currentNode->next;
    }
   
    //系数相同,合并系数操作
        if (currentNode->ceof==p->ceof)
        {

            currentNode->exp=p->exp+currentNode->exp;
            /* code */
        }
        else
        {
               //后继节点
        PolyNode *nextNode = currentNode->next;
        p->next= nextNode;
        currentNode->next = p;
        }
        
   
    
}
void PolyList::printPolyList()
{
    PolyNode *currentNode = head;
    while (currentNode != NULL)
    {
        cout << currentNode->exp << "X"
             << "^" << currentNode->ceof << "+";
        /* code */
        currentNode = currentNode->next;
    }
}
PolyList::~PolyList()
{
}
int main(int argc, char const *argv[])
{

    PolyNode *polyNode;
    polyNode=new PolyNode;
    polyNode->ceof = 1;
    polyNode->exp = 2;
    polyNode->next = NULL;

    PolyList p = PolyList(polyNode);
    p.printPolyList();
    cout<<"\n"<<endl;
    polyNode=new PolyNode;
    polyNode->ceof = 2;
    polyNode->exp = 3;
    polyNode->next = NULL;
    p.addPolyNode(polyNode);
    p.printPolyList();
    cout<<"\n"<<endl;
    polyNode=new PolyNode;
    polyNode->ceof = 2;
    polyNode->exp = 3;
    polyNode->next = NULL;
    p.addPolyNode(polyNode);
    polyNode=new PolyNode;
    polyNode->ceof = 8;
    polyNode->exp = 9;
    polyNode->next = NULL;
    p.addPolyNode(polyNode);
    polyNode=new PolyNode;
    polyNode->ceof = 5;
    polyNode->exp = 4;
    polyNode->next = NULL;
    p.addPolyNode(polyNode);
    p.printPolyList();
    /* code */
    return 0;
}
